#include <QApplication>
#include <QMainWindow>
#include <QAction>
#include <QAction>
#include <QMenuBar>
#include <QWidget>
#include <QToolBar>
#include <QPlainTextEdit>
#include <QFileDialog>
#include <QDockWidget>
#include <QLabel>
#include <QStatusBar>
#include <QGridLayout>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDesktopWidget>
#include <QCheckBox>
#include <QListWidget>
#include <QStackedWidget>
#include <QDir>
#include <QFont>
#include <QIcon>
#include <QStringList>
#include <QMessageBox>
#include <QDebug>
//for xml parse (must add "Qt += xml" to .pro file)
#include <QDomDocument>
#include <QDomElement>
#include <QDomNode>
#include <QDomNodeList>
//for stardard c++
#include <iostream>
using namespace std;

//#include "mainwindow.h"
//#include "reciteword.h"
#include "settingdialog.h"

QStringList SettingDialog::getPaths()
{
  QStringList sl;
  sl << workPathLine->text() + "/" + selectDictLine->text() << speechPathLine->text();
  return sl;
}
void SettingDialog::setPaths(QStringList paths)
{
  workPathLine->setText(paths.at(0).section('/', 0, -2));
  selectDictLine->setText(paths.at(0).section('/', -1, -1));
  speechPathLine->setText(paths.at(1));
}

void SettingDialog::setWorkPath()
{
  QString dir = QFileDialog::getExistingDirectory(this, tr("Select Work Directory"), workPathLine->text(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
  if(dir!=NULL)
    workPathLine->setText(dir);
}

void SettingDialog::setSpeechPath()
{
  QString dir = QFileDialog::getExistingDirectory(this, tr("Select Speech Directory"), speechPathLine->text(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
  if(dir!=NULL)
    speechPathLine->setText(dir);
}

void SettingDialog::selectDict()
{
  QListWidgetItem *item = dictList->currentItem();
  if(item != NULL)
      selectDictLine->setText(item->text());
}

void SettingDialog::dictListDialog()
{
  QDir dir(workPathLine->text());
  QStringList filter;
  QStringList fileList;
  filter << "*.xml";
  fileList = dir.entryList(filter, QDir::Files);
  
  dictList = new QListWidget;
  for(int i=0;i<fileList.length();i++)
    dictList->addItem(fileList.at(i));
  
  QPushButton *btn = new QPushButton(tr("OK"));
  connect(btn, SIGNAL(clicked()), this, SLOT(selectDict()));
  
  QVBoxLayout *layout= new QVBoxLayout;
  layout->addWidget(dictList);
  layout->addWidget(btn);

  QDialog *dia = new QDialog;
  connect(btn, SIGNAL(clicked()), dia, SLOT(accept()));
  dia->setLayout(layout);
  dia->setModal(true);
  dia->show();
  //dia->exec();
}
SettingDialog::SettingDialog(QWidget *parent)
  : QDialog(parent)
{
  //mParent = (MainWindow *)parent;

  setStyleSheet("QPushButton{width:250px;height:60px;font: bold normal 24pt 'Courier New'} QLineEdit{width:400px;height:60px;font:normal normal 24pt 'DejaVu Sans'}");

  workPathLine = new QLineEdit;	//should be the default
  workPathLine->setReadOnly(true);
  QPushButton *workPathButton = new QPushButton(tr("&Work Path"));
  connect(workPathButton, SIGNAL(clicked()), this, SLOT(setWorkPath()));

  selectDictLine = new QLineEdit;
  selectDictLine->setReadOnly(true);
  QPushButton *selectDictButton = new QPushButton(tr("&Select Dict"));
  connect(selectDictButton, SIGNAL(clicked()), this, SLOT(dictListDialog()));
    
  speechPathLine = new QLineEdit;
  speechPathLine->setReadOnly(true);
  QPushButton *speechPathButton = new QPushButton(tr("&Speech Path"));
  connect(speechPathButton, SIGNAL(clicked()), this, SLOT(setSpeechPath()));

  QPushButton *ret = new QPushButton(tr("&Return"));
  //  connect(ret, SIGNAL(clicked()), parent, SLOT(returnToRecite()));
  connect(ret, SIGNAL(clicked()), parent, SLOT(showRecitePage()));
  
  QGridLayout *diaLayout = new QGridLayout;
  diaLayout->setSpacing(20);
  diaLayout->addWidget(workPathLine, 0, 0);
  diaLayout->addWidget(workPathButton, 0, 1);
  diaLayout->addWidget(selectDictLine, 1, 0);
  diaLayout->addWidget(selectDictButton, 1, 1);
  diaLayout->addWidget(speechPathLine, 2, 0);
  diaLayout->addWidget(speechPathButton, 2, 1);
  diaLayout->addWidget(ret, 3, 0, 1, 2);
  //  setLayout(diaLayout);

  QAction *quitAct = new QAction(tr("&Quit"), this);
  //QFont userFont("Courier New", 18, QFont::Normal);//QFont::Bold);userFont);
  quitAct->setFont(QFont("Courier New", 18, QFont::Normal));
  connect(quitAct, SIGNAL(triggered()), parent, SLOT(close()));
  QToolBar *tb = new QToolBar;
  tb->addAction(quitAct);

  QGridLayout *gl = new QGridLayout;  
  gl->addWidget(tb, 0, 0, 1, 3, Qt::AlignTop);
  gl->setRowStretch(0, 300);
  gl->setRowStretch(2, 500);
  gl->setColumnStretch(0, 800);
  gl->setColumnStretch(2, 800);
  gl->addLayout(diaLayout, 1, 1);
  setLayout(gl);
}

